package net.chathu.airgw.util 
{
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author ze
	 */
	public class ByteArrayDump 
	{
		
		public function ByteArrayDump() 
		{
			
		}
		
		public static function dumpArray(array:Array):void
		{
			
			var len:int = array.length;
			var str:String = "";
			var txtstr:String = "";
			for (var i:int = 0; i < len; i++)
			{
				//trace();
				var char:int = (array[i] +256)%256;
				str += d2h(char) + " ";
				txtstr += char==0 ? " " : String.fromCharCode(char);
				if (!((i + 1) % 8) || i + 1 == len) {
					txtstr = txtstr.replace("\n","?").replace("\r", "?");
					trace(Math.ceil((i+1) / 8) + " | \t" + str + "                            ".substr(0,(8-(i + 1) % 8)*3)+" \t" + txtstr);
					str = "";
					txtstr = "";
				}
			}
			
		}
		
		public static function dump(ba:ByteArray):void
		{
			var cp:int = ba.position;
			ba.position = 0;
			var len:int = ba.bytesAvailable;
			var str:String = "";
			var txtstr:String = "";
			for (var i:int = 0; i < len; i++)
			{
				//trace();
				var char:int = (ba.readByte() +256)%256;
				str += d2h(char) + " ";
				txtstr += char==0 ? " " : String.fromCharCode(char);
				if (!((i + 1) % 8) || i + 1 == len) {
					txtstr = txtstr.replace("\n", "?").replace("\r", "?");
					//..csak gyors tesztre:
					trace(Math.ceil((i+1) / 8) + " | \t" + str + "                            ".substr(0,(8-(i + 1) % 8)*3)+" \t" + txtstr);
					str = "";
					txtstr = "";
				}
			}
			ba.position = cp;
		}
		
		
		private static function dec2hex( dec:String ) : String {
			var hex:String = "0x";
			var bytes:Array = dec.split(" ");
			for( var i:int = 0; i <bytes.length; i++ )
			hex += d2h( int(bytes[i]) );
			return hex;
		}
		
		private static function d2h( d:int ) : String {
			var c:Array = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ];
			if( d> 255 ) d = 255;
			var l:int = d / 16;
			var r:int = d % 16;
			return c[l]+c[r];
		}
	}

}